<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
// $cates = pdo_fetchall("SELECT * FROM " . tablename('zh_yjm_storetype'));
// $cur = pdo_get('zh_yjm_store',array('id'=>$_GPC['id']));
// $c_id=$cur['storetype_id'];
// function gethtmlTree($data, $pId,$num=0,$c_id)
// {
// $html = '';
// $num++;
//   foreach($data as $k => $v)
//   {
//      if($v['p_id'] == $pId)
//      {
//       $html .= '<option value="'.$v['id'].'"'.(($v['id']== $c_id) ? 'selected="selected"' : '') . '>'.str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',$num-1)."|-".$v['title'];
//       $html .= gethtmlTree($data, $v['id'],$num,$c_id);
//       $html = $html."</option>";
//      }
//   }
// return $html;
// }
// $cates=gethtmlTree($cates, 0,$num,$c_id);

// $item = pdo_get('zh_yjm_store',array('id'=>$_GPC['id']));

$storetype = pdo_fetchall("SELECT * FROM " . tablename('zh_yjm_storetype'));
$item = pdo_get('zh_yjm_store',array('id'=>$_GPC['id']));
if ($item) {
    $info['imgs']=explode(',',$item['imgs']);
}
if(checksubmit('submit')){
        if (empty($_GPC['admin']) || empty($_GPC['password'])) {
            message('请填写账号密码！','','error');
        }
        $data['sort']=$_GPC['sort'];
        $data['storetype_id']=$_GPC['storetype_id'];
        $data['img']=$_GPC['img'];
        $data['store']=$_GPC['store'];
        $data['name']=$_GPC['name'];
        $data['phone']=$_GPC['phone'];
        $data['area']=$_GPC['area'];
        $data['latlng']=$_GPC['latlng'];
        $data['admin']=$_GPC['admin'];
        $data['password']=$_GPC['password'];
        $data['imgs']=implode(",",$_GPC['imgs']);
        $data['detail']=html_entity_decode($_GPC['detail']);
        $data['rule']=html_entity_decode($_GPC['rule']);
        $data['time']=date('Y-m-d H:i:s',time());
        $data['uniacid']=$_W['uniacid'];
        if($_GPC['id']==''){  
            if (user_check(array('username' => $_GPC['admin']))) {
                message('非常抱歉，此登录账号已存在');
            }
            $rst=pdo_insert('zh_yjm_store',$data);      
            if($rst){
                    $user['username']=$_GPC['admin'];
                    $user['password']=$_GPC['password'];
                    $user['salt'];
                    $uid = user_register($user,null);
                    if ($uid > 0) {
                        unset($user['password']);
                        $datas = array(
                            'uniacid' => $_W['uniacid'],
                            'uid' => $uid,
                            'role' => 'operator',
                        );
                        $exists = pdo_fetch("SELECT * FROM " . tablename('uni_account_users') . " WHERE uid = :uid AND uniacid = :uniacid", array(':uniacid' => $_W['uniacid'], ':uid' => $uid));
                        if (empty($exists)) {
                            pdo_insert('uni_account_users', $datas);
                        }
                        //permission
                        pdo_insert('users_permission', array(
                            'uid' => $uid,
                            'uniacid' => $_W['uniacid'],
                            'url' => '',
                            'type' => 'zh_yjm',
                            'permission' => 'zh_yjm_menu_store'
                        ));
                    }
                 message('添加成功！', $this->createWebUrl('store'), 'success');
            }else{
                 message('添加失败！','','error');
            }
        }else{
            $lib = pdo_get('zh_yjm_store',array('id'=>$_GPC['id']));
            $adminpassword = pdo_get('users',array('username'=>$lib['admin']));
            if ($lib['admin']==$_GPC['admin'] && $lib['password']==$_GPC['password']) {
                $rst=pdo_update('zh_yjm_store',$data,array('id'=>$_GPC['id']));
                if($rst){
                     message('编辑成功！', $this->createWebUrl('store'), 'success');
                }else{
                     message('编辑失败！','','error');
                }
            }
            if ($lib['admin']!=$_GPC['admin'] && $lib['password']==$_GPC['password']) {
                pdo_update('users', array('username' => $_GPC['admin']), array('uid' => $adminpassword['uid']));
                $rst=pdo_update('zh_yjm_store',$data,array('id'=>$_GPC['id']));
                if($rst){
                     message('编辑成功！', $this->createWebUrl('store'), 'success');
                }else{
                     message('编辑失败！','','error');
                }
            }
            if ($lib['password']!=$_GPC['password'] && $lib['admin']==$_GPC['admin']) {
                pdo_update('users', array('password' => user_hash($_GPC['password'], $adminpassword['salt'])), array('uid' => $adminpassword['uid']));
                $rst=pdo_update('zh_yjm_store',$data,array('id'=>$_GPC['id']));
                if($rst){
                     message('编辑成功！', $this->createWebUrl('store'), 'success');
                }else{
                     message('编辑失败！','','error');
                }
            }
            if ($lib['password']!=$_GPC['password'] && $lib['admin']!=$_GPC['admin']) {
                pdo_update('users', array('username' => $_GPC['admin'],'password' => user_hash($_GPC['password'], $adminpassword['salt'])), array('uid' => $adminpassword['uid']));
                $rst=pdo_update('zh_yjm_store',$data,array('id'=>$_GPC['id']));
                if($rst){
                     message('编辑成功！', $this->createWebUrl('store'), 'success');
                }else{
                     message('编辑失败！','','error');
                }
            }
            if (empty($lib['admin'])) {//不会执行了
                $rst=pdo_update('zh_yjm_store',$data,array('id'=>$_GPC['id']));
                if($rst){
                        $user['username']=$_GPC['admin'];
                        $user['password']=$_GPC['password'];
                        $user['salt'];
                        $uid = user_register($user,null);
                        if ($uid > 0) {
                            unset($user['password']);
                            $datas = array(
                                'uniacid' => $_W['uniacid'],
                                'uid' => $uid,
                                'role' => 'operator',
                            );
                            $exists = pdo_fetch("SELECT * FROM " . tablename('uni_account_users') . " WHERE uid = :uid AND uniacid = :uniacid", array(':uniacid' => $_W['uniacid'], ':uid' => $uid));
                            if (empty($exists)) {
                                pdo_insert('uni_account_users', $datas);
                            }
                            //permission
                            pdo_insert('users_permission', array(
                                'uid' => $uid,
                                'uniacid' => $_W['uniacid'],
                                'url' => '',
                                'type' => 'zh_yjm',
                                'permission' => 'zh_yjm_menu_store'
                            ));
                        }
                     message('编辑成功！', $this->createWebUrl('store'), 'success');
                }else{
                     message('编辑失败！','','error');
                }
            }
        }
}
include $this->template('web/addstore');